home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3743 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.0 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: End of File
  5. Date: 30 Jan 1996 23:35:23 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan30163523@qcd.lanl.gov>
  8. References: <4edr5q$8fi@baloo.pipex-sa.net> <310E5B7B.4610@microsports.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: "Eric W. Bradway"'s message of Tue, 30 Jan 1996 12:55:07 -0500
  13.  
  14. In article <310E5B7B.4610@microsports.com> "Eric W. Bradway"
  15. <ebradway@microsports.com> writes: 
  16.    Unregistered wrote:
  17.    > 
  18.    > How does one set the file pointer to end of file on a binary file in C.
  19.  
  20.    fseek(file, 0L, SEEK_END);
  21.  
  22. There is no portable solution: the above will work on some
  23. implementations. 
  24.  
  25. The basic problem is that the `end' of a binary file is not a well
  26. defined concept. As arbitrary characters can be written to a binary
  27. file with the expectation of being able to read back unchanged, it may
  28. not be able to support `out of band' data to mark the end of the file
  29. without additional overhead. This end of file determination is often
  30. (though it is certainly not required to be) handled at the OS level
  31. which may try to minimize this overhead by allowing files of only
  32. certain sizes (e.g. multiples of a logical block etc.) The C standard
  33. allows for this by saying that when a binary file is written and read
  34. back, additional '\0' (i.e. null characters) may be read back which
  35. were never written out: in other words, the implementation is allowed
  36. to pad the file with zeros. (The implementation has to document how
  37. many zeros are added if any.) This results in an ambiguity in the
  38. definition of the end of file in a binary file (because the zeros may
  39. be added only when the file is closed and not before), and the
  40. implementation is allowed to not meaningfully support the concept of
  41. an end of file for a binary file.
  42.  
  43. For text files, as only printable characters, '\t' and '\n' can
  44. be written with the expectation of being read back unchanged (with
  45. further restrictions irrelevant to our discussion: required '\n' at
  46. the end of file and no blanks at end of line; the implementation has
  47. to document what happens if the second of these is violated), the
  48. implementation has a way of marking the end of file very simply (say
  49. with a '\0' or '\032'). Historically, therefore, text files could be
  50. of any length: and the C standard requires the end of file to be a
  51. meaningful concept. In this case however, the usual end of line on the
  52. system may be different from that required by the C standard (i.e. eol
  53. may be represented by something other than '\n'), which means that
  54. characters in a text file may not `stand for themselves'. So the
  55. standard allows the offsets not to be counted in real characters (for
  56. example they could easily be the number of bytes in the external
  57. representation of the file, or in the internal): all that is required
  58. is that ftell and fseek use the same method of counting (naturally). A
  59. value of zero however has a fixed meaning in any method of counting,
  60. and is allowed. So, the fseek call above would have worked for a text
  61. file.
  62.  
  63. Note that technically the above refers to text streams and binary
  64. streams. An implementation may or may not have separate concepts for
  65. text and binary `files', and if it supports the concept, an fopen of a
  66. binary file as a text stream (or vice-versa) might fail.
  67.  
  68. Everything above is to rationalize the rules of ANSI C, to make them
  69. understandable (like mnemonics). I do not claim that they reflect any
  70. or all of historical basis of the rules.
  71.  
  72. Cheers
  73. Tanmoy
  74. --
  75. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  76. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  77. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  78. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  79. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  80. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  81.